using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace RunAliasCreator
{
	public partial class RunAliasForm : Form
	{
		public static string GetFileRunAlias(string filename)
		{
			int endPos = filename.LastIndexOf('\\');
			string fileTitle = filename.Substring(endPos + 1);
			if (fileTitle == string.Empty)
			{
				int begPos = filename.LastIndexOf('\\', endPos - 1) + 1;
				fileTitle = filename.Substring(begPos, endPos - begPos);
			}
			endPos = fileTitle.LastIndexOf('.');
			if (endPos > 0)
				fileTitle = fileTitle.Substring(0, fileTitle.LastIndexOf('.'));
			fileTitle = fileTitle.Replace(" ", string.Empty);
			fileTitle = fileTitle.Replace(":", string.Empty);
			fileTitle = fileTitle.Replace("*", string.Empty);
			fileTitle = fileTitle.Replace("?", string.Empty);
			fileTitle = fileTitle.Replace("\"", string.Empty);
			fileTitle = fileTitle.Replace("|", string.Empty);
			fileTitle = fileTitle.Replace("/", string.Empty);
			fileTitle = fileTitle.Replace("\\", string.Empty);
			fileTitle = fileTitle.Replace("<", string.Empty);
			fileTitle = fileTitle.Replace(">", string.Empty);
			return fileTitle;
		}

		public void SelectFile()
		{ SelectFile(null, null); }
		public void SelectFile(string filename)
		{ SelectFile(filename, null); }
		public void SelectFile(string filename, string alias)
		{
			if (String.IsNullOrEmpty(filename))
			{
				targetValue.Text = string.Empty;
				runAliasValue.Text = string.Empty;
				targetValue.Focus();
			}
			else
			{
				targetValue.Text = filename;
				if (String.IsNullOrEmpty(alias))
					runAliasValue.Text = GetFileRunAlias(filename);
				else
					runAliasValue.Text = alias;
				targetValue.SelectAll();
				runAliasValue.SelectAll();
				runAliasValue.Focus();
			}
		}

		public RunAliasForm()
		{
			InitializeComponent();
		}
		public RunAliasForm(string filename)
			: this(filename, null) { }
		public RunAliasForm(string filename, string alias)
			: this()
		{
			SelectFile(filename, alias);

		}

		private void createButton_Click(object sender, EventArgs e)
		{
			if (!File.Exists(targetValue.Text))
			{
				MessageBoxes.Exclamation("The target specified does not exist.");
				targetValue.Focus();
				return;
			}
			else if (runAliasValue.Text == string.Empty)
			{
				MessageBoxes.Exclamation("A run alias must be specified.");
				runAliasValue.Focus();
				return;
			}
			try
			{
				string runTarget = targetValue.Text;
				string runAlias = runAliasValue.Text;
				if (RunAlias.Exists(runAlias))
				{
					if (MessageBoxes.Question("A run alias with this name already exists.\n\nWould you like to overwrite it?") == DialogResult.No)
						return;
				}
				RunAlias.Create(runTarget, runAlias, true);
			}
			catch (Exception ex)
			{
				MessageBoxes.Exclamation("The run alias could not be created because an exception occurred.\n\n" + Errors.SummarizeException(ex));
			}
			MessageBoxes.Information("The run alias was successfully created.");
			targetValue.SelectAll();
			runAliasValue.SelectAll();
			targetValue.Focus();
		}

		private void RunAliasForm_Load(object sender, EventArgs e)
		{
			Show();
			if (String.IsNullOrEmpty(targetValue.Text))
				targetValue.Focus();
			else
				runAliasValue.Focus();
		}

		private void targetBrowseButton_Click(object sender, EventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.DereferenceLinks = false;
			dialog.Filter = "Executable Files (*.exe, *.cmd, *.bat, *.com, *.pif)|*.exe;*.cmd;*.bat;*.com;*.pif|All Files (*.*)|*.*";
			if (String.IsNullOrEmpty(targetValue.Text))
				dialog.FileName = targetValue.Text;
			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.Cancel) return;
			SelectFile(dialog.FileName);
		}

		private void RunAliasForm_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
				e.Effect = DragDropEffects.All;
		}

		private void RunAliasForm_DragDrop(object sender, DragEventArgs e)
		{
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			if (files == null || files.Length == 0) return;
			SelectFile(files[0]);
		}

	}
}